package com.wanxi.interceptor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.domain.ResultModel;
import com.wanxi.domain.product.ProductModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;

/*会拦截所有被@RequestBody修饰的类*/
@ControllerAdvice
public class MyGlobalHandler implements ResponseBodyAdvice {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *
     * @param methodParameter
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
//        /*判断用户是否已经登录*/
//        if(redisTemplate.hasKey("loginFlag")){
//            /*删除登录键*/
////            redisTemplate.delete("loginFlag");
//            /*已经登录则不用继续对返回的json进行封装*/
//            return false;
//        }
        return true;
    }

    /*可用于对返回到客户端之前的json进行封装转发*/
    /**
     *
     * @param o 被拦截的控制器原本返回来json对象
     * @param methodParameter
     * @param mediaType
     * @param aClass
     * @param serverHttpRequest
     * @param serverHttpResponse
     * @return
     */
    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

        //用于封装控制器的返回对象
        ResultModel resultModel = new ResultModel();
        resultModel.setData(o);

        if(o instanceof Exception){
            resultModel.setCode(500);
            resultModel.setMsg("error");
            resultModel.setData(o);
            return resultModel;
        }


        //用户已经登录
        if(redisTemplate.hasKey("login")){
            resultModel.setMsg("success");
            resultModel.setCode(200);
            resultModel.setData(o);
            /*判断返回类型是否属于List<ProductModel>*/
            if(o instanceof List
                    &&
                    ((List) o).get(0) instanceof ProductModel){

                resultModel.setCount(((ProductModel) ((List) o).get(0)).getNum());
            }
        }else {
            //用户未登录将code设为403
            resultModel.setCode(403);
        }

        if(o instanceof String){
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                //控制层返回字符串时，由于转换器的原因，直接返回包装该字符串的对象会报错
                //需要手动将要返回给适配器的对象转成json字串
                String  result = objectMapper.writeValueAsString(resultModel);
                return result;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        /*在进行封装时不知道为啥一定得把id查出来*/
        return resultModel;
    }
}
